<?php
	session_start();
	include "../Exam.php";
	include "../Questionnaire.php";
	
	$ans=$_POST['answers'];
	$json=json_decode($ans,TRUE);
	$res=new Exam();
	$ques = new Questionnaire();
	$wrong_answers = Array();
	foreach($json as $id => $value)
	{
		if(!$res->check($id,$value)) {
			$wrong_answers[]=$id;
		}
	}

	?>
	
	<h1>Results</h1>
	<table>
		<tr>
			<td>Questions</td><td><?php echo $_POST['maxmarks']; ?></td>
		</tr>
		<tr>
			<td>Answered Questions</td><td><?php echo count($json); ?></td>
		</tr>
		<tr>
			<td>Unanswered Questions</td><td><?php echo ($_POST['maxmarks']-count($json)); ?></td>
		</tr>
		<tr>
			<td>Correct Answers</td><td><?php echo $res->getScore(); ?></td>
		</tr>
		<tr>
			<td>Wrong Answers</td><td><?php echo $res->getWrongAns(); ?></td>
		</tr>
	</table>
	<?php if($res->getWrongAns()>0) { ?>
	<h2>Explanation for wrong answers</h2>
	<div id="wrong-answers">
	<?php
	foreach($wrong_answers as $qid) {
		echo "<h3 class='question'>".$ques->getQuestion($qid)."</h3>";
		echo "<span>a)".$ques->getOption1($qid)."</span>";
		echo "<span>b)".$ques->getOption2($qid)."</span>";
		echo "<span>c)".$ques->getOption3($qid)."</span>";
		echo "<span>d)".$ques->getOption4($qid)."</span>";
		echo "<div>Correct answer:<span class='correct-answer'>".$ques->getCorrectAns($qid)."</span></div>";
		echo "<h3>Explanation</h3>";
		echo "<div>".$ques->getExplanation($qid)."</div>";
	}
	?>
	</div>
	<?php } ?>
	<?php	

	/*echo json_encode($wrong_answers);
	echo "No of Questions:".$_POST['maxmarks'];
	echo "No of Answered Questions:".count($json);
	echo "No of Unanswered Questions:".($_POST['maxmarks']-count($json));
	echo "No of Correct Answers:".$res->getScore();
	echo "No of Wrong Answers:".$res->getWrongAns();*/
	
	$examid=$res->getEid();
	if($examid!="avail")
	{
		$result=$res->result($_SESSION['username'],$examid);
	}
	else
	{
		echo "examid not found";
	}
?>

